package ru.paswd.marketprice.ui.activity

import android.Manifest
import android.view.View
import com.budiyev.android.codescanner.AutoFocusMode
import com.budiyev.android.codescanner.CodeScanner
import com.budiyev.android.codescanner.ScanMode
import com.google.zxing.BarcodeFormat
import kotlinx.android.synthetic.main.activity_receipt_scan.*
import kotlinx.android.synthetic.main.layout_scan_result.*
import moxy.presenter.InjectPresenter
import ru.paswd.marketprice.R
import ru.paswd.marketprice.ui.presenter.ReceiptScanPresenter
import ru.paswd.marketprice.ui.view.ReceiptScanView
import ru.paswd.marketprice.utils.SystemUtils

class ReceiptScanActivity : BaseActivity(), ReceiptScanView {

    @InjectPresenter
    lateinit var presenter: ReceiptScanPresenter

    private lateinit var codeScanner: CodeScanner
    private var codeScannerEnabled = false

    override fun initInterface() {
        if (!SystemUtils.isCameraExists(applicationContext)) {
            showMessage(R.string.notification_camera_not_found)
            finish()
            return
        }

        buttonReturn.setOnClickListener(this)
        getCameraPermission()
    }

    override fun setAttr() {
        layout = R.layout.activity_receipt_scan
    }

    override fun showProgressBar() {
        codeScannerView.visibility = View.GONE
        scanProgressBar.visibility = View.VISIBLE
    }

    override fun setResultSuccess() {
        layoutScanResultSuccess.visibility = View.VISIBLE
    }

    override fun setResultFailed() {
        layoutScanResultFailed.visibility = View.VISIBLE
    }

    override fun closeView() {
        finish()
    }

    override fun onRefreshed() {
        scanProgressBar.visibility = View.GONE
        showResult()
    }

    override fun onResume() {
        super.onResume()

        if (codeScannerEnabled) {
            codeScanner.startPreview()
        }
    }

    override fun onPause() {
        if (codeScannerEnabled) {
            codeScanner.releaseResources()
        }

        super.onPause()
    }

    override fun getPermissionRequestCode() = PERMISSIONS_REQUEST_ACCESS_CAMERA

    override fun onPermissionsGranted() {
        codeScannerInit()
    }

    override fun onClick(view: View?) {
        when (view?.id) {
            R.id.buttonReturn -> presenter.onBackPressed()
        }
    }

    override fun stopScanning() {
        if (codeScannerEnabled) {
            codeScanner.releaseResources()
            codeScannerEnabled = false
        }
    }

    private fun getCameraPermission() {
        getPermission(Manifest.permission.CAMERA)
    }

    private fun codeScannerInit() {
        codeScanner = CodeScanner(this, codeScannerView)
        codeScanner.camera = CodeScanner.CAMERA_BACK
        codeScanner.formats = listOf(BarcodeFormat.QR_CODE)
        codeScanner.autoFocusMode = AutoFocusMode.SAFE
        codeScanner.scanMode = ScanMode.SINGLE
        codeScanner.isAutoFocusEnabled = true
        codeScanner.isFlashEnabled = false

        codeScanner.setDecodeCallback { r ->
            runOnUiThread { presenter.qrCodeScannedSuccess(r.text) }
        }
        codeScanner.setErrorCallback { ex ->
            runOnUiThread { presenter.qrCodeScannedWithError(ex.message) }
        }

        codeScanner.startPreview()

        codeScannerEnabled = true
    }

    private fun showResult() {
        layoutScanResult.visibility = View.VISIBLE
    }
}